<?php
require_once 'DBConnecter.inc';
class Outgoings extends DBConnecter {
	public $mem_id = "";
	public $mem_nm = "";
	public $etc_amt_1 = "";
	public $etc_amt_2 = "";
	public $etc_amt_3 = "";
	public $subs_firm = "";
	public $subs_amt = "";
	public $sum_subs_amt = "";
	public $sum_etc_amt_1 = "";
	public $sum_etc_amt_2 = "";
	public $sum_etc_amt_3 = "";
	public $sum_ded_amt = "";
	public $ect_cnt = "";
	public $subs_cnt = "";
	public $loop_cnt = "";
	function __construct() {
		$this->connectDB ();
		$mem_id = array ();
		$mem_nm = array ();
		$etc_amt_1 = array ();
		$etc_amt_2 = array ();
		
		$subs_firm = array ();
		$subs_amt = array ();
		
		$sum_subs_amt = 0;
		$sum_etc_amt_1 = 0;
		$sum_etc_amt_2 = 0;
		$sum_etc_amt_3 = 0;
	}
	public function makeSum($val) {
		switch ($val) {
			case 1 :
				echo "<tr><td colspan='2'>지급액 합계:</td><td>" . Number_Format ( $this->sum_subs_amt ) . "</td><td>" . Number_Format ( $this->sum_etc_amt_1 ) . "</td><td>" . Number_Format ( $this->sum_etc_amt_2 ) . "</td></tr>";
				break;
			case 2 :
				echo "<tr><td colspan='2'>용차비용 합계:</td><td colspan='3'>" . Number_Format ( $this->sum_ded_amt ) . "</td></tr>";
				break;
		}
	}
	public function makeTableWrapper($val) {
		switch ($val) {
			case 1 :
				echo "<table class='table table-bordered'>";
				echo "<thead><tr><th colspan='5'>운송비 지출</th></tr><tr><th>사원번호</th><th>성명</th><th>운송료</th><th>공제</th><th>지급액</th></tr></thead>";
				echo "<tbody>";
				break;
			case 2 :
				echo "</tbody></table><table class='table table-bordered'>";
				echo "<thead><tr><th colspan='5'>용차비 지출</th></tr><tr><th colspan='2'>용차업체</th><th colspan='3'>용차비용</th></tr></thead>";
				echo "<tbody>";
				break;
			case 3 :
				echo "</tbody></table>";
				break;
		}
	}
	public function printSubsAmt() {
		$loop_cnt = 0;
		$length = count ( $this->subs_firm );
		// echo "subs=".$length;
		while ( $loop_cnt < $length ) {
			echo "<tr>";
			echo "<td colspan='2'>" . $this->subs_firm [$loop_cnt] . "</td>";
			echo "<td>" . Number_Format ( $this->subs_amt [$loop_cnt] ) . "</td>";
			echo "</tr>";
			
			$loop_cnt ++;
		}
	}
	public function printEctAmt() {
		$loop_cnt = 0;
		$length = count ( $this->mem_id );
		// echo "etc=".$length;
		while ( $loop_cnt < $length ) {
			echo "<tr>";
			echo "<td>" . $this->mem_id [$loop_cnt] . "</td>";
			echo "<td>" . $this->mem_nm [$loop_cnt] . "</td>";
			echo "<td>" . Number_Format ( $this->etc_amt_1 [$loop_cnt] ) . "</td>";
			echo "<td>" . Number_Format ( $this->etc_amt_2 [$loop_cnt] ) . "</td>";
			echo "<td>" . Number_Format ( $this->etc_amt_3 [$loop_cnt] ) . "</td>";
			echo "</tr>";
			
			$loop_cnt ++;
		}
	}
	public function search4table($dt) {
		$sql = "CALL ".self::CONST_DB.".expend_detail_search('" . $dt . "');select @out_err_cd, @out_err_msg;";
		// echo $sql;
		$retval = null;
		$retval = array ();
		$ect_cnt = 0;
		$subs_cnt = 0;
		
		if ($rs = $this->getMySqlConneter ()->multi_query ( $sql )) {
			if ($rs != null && $rs = $this->getMySqlConneter ()->use_result ()) {
				while ( $row = $rs->fetch_row () ) {
					if (strcmp ( $row ['0'], "sum" ) == 0) {
						// echo "sum!!!<br>";
						$this->sum_subs_amt = $row ['1'];
						$this->sum_etc_amt_1 = $row ['2'];
						$this->sum_etc_amt_2 = $row ['3'];
						$this->sum_etc_amt_3 = $row ['4'];
						$this->sum_ded_amt = $row ['5'];
					} else if (strcmp ( $row ['0'], "subs" ) == 0) {
						// echo "subs!!!<br>";
						$this->subs_firm [$subs_cnt] = $row ['1'];
						$this->subs_amt [$subs_cnt] = $row ['2'];
						
						$subs_cnt ++;
					} else {
						// echo "ect!!!<br>";
						$this->mem_id [$ect_cnt] = $row ['1'];
						$this->mem_nm [$ect_cnt] = $row ['2'];
						$this->etc_amt_1 [$ect_cnt] = $row ['3'];
						$this->etc_amt_2 [$ect_cnt] = $row ['4'];
						$this->etc_amt_3 [$ect_cnt] = $row ['5'];
						
						$ect_cnt ++;
					}
				}
				$rs->free ();
				
				$this->makeTableWrapper ( 1 );
				$this->printEctAmt ();
				$this->makeSum ( 1 );
				$this->makeTableWrapper ( 2 );
				$this->printSubsAmt ();
				$this->makeSum ( 2 );
				$this->makeTableWrapper ( 3 );
			}
		}
	}
}